
use test1;
use std::fmt;

struct City {
    name: &'static str,
    lat: f32,
    lon: f32,
}
impl fmt::Display for City {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
        let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };
        write!(f, "{}: {:.3}°{} {:.3}°{}",
               self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
    }
}

#[derive(Debug)]
struct Color {
    red: u8,
    green: u8,
    blue: u8,
}
impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RGB ({0}, {1}, {2}) 0x{:02X}{:02X}{:02X}", self.red, self.green, self.blue)
    }
}

/// 文档注释:
/// 注释, 格式化打印
fn main() {

    // 行注释, 编译器忽略
    /*
     * 段落注释
     */
    /* 段落注释 */
    //! 文档注释

    // 输出
    let a = format!("Hello, world!");
    println!("{}", a);
    println!("3+1={}", test1::add_one(3));
    print!("{0}{0}{1}{1}", "a", "b");
    println!("{a}{b}", a="1", b=2);

    eprint!("err1");
    eprintln!("err2");

    println!("10 binary is {:b}. 11 hex is {:x}", 10, 11);

    // 数字格式化
    println!("{number:>width$}", number=1, width=6);
    println!("{number:>0width$}", number=1, width=6);

    // 保留小数位输出
    println!("Pi is roughly {pi:.*}", 2, pi = 3.141592);
    println!("Pi is roughly {0:.3}", 3.141592);
    println!("Pi is roughly {0:.1$}", 3.141592, 3);
    println!("Pi is roughly {1:.*}", 3, 3.141592);
    println!("Pi is roughly {number:.prec$}", prec = 4, number = 3.141592);

    // 单字段结构体
    #[derive(Debug)]
    struct Structure(i32);
    impl fmt::Display for Structure{
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "is :{:}", self.0)
        }
    }

    println!("This struct {}", Structure(3));
    println!("This struct {:?}", Structure(354));

    // 多字段结构体
    #[derive(Debug)]
    struct Complex{
        real: f64,
        imag: f64,
    }
    impl fmt::Display for Complex{
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{} + {}i", self.real, self.imag)
        }
    }
    let complex =Complex {real: 3.3, imag: 7.2};
    println!("Display: {}", complex);
    println!("Debug: {:?}", complex);

    // 数组结构体
    #[derive(Debug)]
    struct List(Vec<i32>);
    impl fmt::Display for List {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            let vec = &self.0;
            write!(f, "[")?;
            for (count, v) in vec.iter().enumerate() {
                if count != 0 {
                    write!(f, ", ")?;
                }
                write!(f, "{}:{}", count, v)?;
            }
            write!(f, "]")
        }
    }
    let list = List(vec![1, 2, 3]);
    println!("{}", list);
    println!("Debug: {:#?}", list);

    // 格式化输出 Display trait
    for city in [
        City { name: "Dublin", lat: 53.347778, lon: -6.259722 },
        City { name: "Oslo", lat: 59.95, lon: 10.75 },
        City { name: "Vancouver", lat: 49.25, lon: -123.1 },
    ].iter() {
        println!("{}", *city);
    }
    for color in [
        Color { red: 128, green: 255, blue: 90 },
        Color { red: 0, green: 3, blue: 254 },
        Color { red: 0, green: 0, blue: 0 },
    ].iter() {
        // println!("{:?}", *color);
        println!("{}", *color);
    }

}
